package com.studies.pc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studies.pc.mapper.BaseInfoWithBlobsMapper;
import com.studies.pc.mapper.EvaluateInfoMapper;
import com.studies.pc.pojo.BaseInfo;
import com.studies.pc.mapper.BaseInfoMapper;
import com.studies.pc.pojo.BaseInfoWithBlobs;
import com.studies.pc.pojo.EvaluateInfo;
import com.studies.pc.service.BaseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studies.pc.query.BaseInfoQuery;
import com.studies.pc.service.BaseInfoWithBlobsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 基地 服务实现类
 * </p>
 *
 * @author 研学活动后台
 * @since 2019-12-24
 */
@Service
public class BaseInfoServiceImpl extends ServiceImpl<BaseInfoMapper, BaseInfo> implements BaseInfoService {
    @Autowired
    private BaseInfoWithBlobsMapper baseInfoWithBlobsMapper;
    @Autowired
    private EvaluateInfoMapper evaluateInfoMapper;
    @Override
    public Map<String, Object> qryBaseInfoPage(Page<BaseInfo> pageParam, BaseInfoQuery baseInfoQuery) {
        //首页基地分页实现
        QueryWrapper<BaseInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.orderByDesc("update_time");
        if (baseInfoQuery.getAreaId()!=null){
            queryWrapper.eq("area_id",baseInfoQuery.getAreaId());
        }
        if (baseInfoQuery.getBaseName()!=null){
            queryWrapper.like("base_name",baseInfoQuery.getBaseName());
        }
        if (baseInfoQuery.getBaseTags()!=null){
            queryWrapper.eq("base_tags",baseInfoQuery.getBaseTags());
        }
        if (baseInfoQuery.getBaseType()!=null){
            queryWrapper.eq("base_type",baseInfoQuery.getBaseType());
        }
        baseMapper.selectPage(pageParam,queryWrapper);
        List<BaseInfo> records =pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public BaseInfoWithBlobs qryBaseInfoWithBlobs(Integer id) {
        BaseInfoWithBlobs baseInfoWithBlobs = baseInfoWithBlobsMapper.selectById(id);
        return baseInfoWithBlobs;
    }

    @Override
    public EvaluateInfo qryBaseEvaluateInfo(Integer id) {
        EvaluateInfo evaluateInfo = evaluateInfoMapper.qryBaseEvaluateInfo(id);
        return evaluateInfo;
    }

    @Override
    public Map<String, Object> qryBaseEvaluatePage(Page<EvaluateInfo> pageParam, Integer id) {
        QueryWrapper<BaseInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.orderByDesc("update_time");
        if (id!=null){
            queryWrapper.eq("base_id",id);
        }
        evaluateInfoMapper.qryBaseEvaluatePage(pageParam,id);
        List<EvaluateInfo> records =pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
